package com.neuedu.his.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.neuedu.his.po.Checkapply;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface CheckapplyMapper extends BaseMapper<Checkapply> {
    // 使用MyBatis-Plus的注解实现复杂查询

    @Select("SELECT * FROM checkapply WHERE state = 2 AND itemid IN " +
            "(SELECT ID FROM fmeditem WHERE deptid = #{deptId}) " +
            "ORDER BY isurgent, creationtime")
    List<Checkapply> selectPendingByDept(@Param("deptId") Integer deptId);

    @Update("<script>" +
            "UPDATE checkapply SET state = #{state} WHERE id IN " +
            "<foreach item='id' collection='ids' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int batchUpdateState(@Param("ids") List<Integer> ids, @Param("state") Integer state);

    @Update("UPDATE checkapply SET state = 3, checkoperid = #{operatorId}, checktime = NOW() " +
            "WHERE id = #{checkApplyId}")
    int updateToChecking(@Param("checkApplyId") Integer checkApplyId,
                         @Param("operatorId") Integer operatorId);

    @Select("SELECT * FROM checkapply WHERE itemid IN " +
            "(SELECT ID FROM fmeditem WHERE deptid = #{deptId}) " +
            "ORDER BY isurgent, creationtime")
    List<Checkapply> selectByDept(@Param("deptId") Integer deptId);

    @Select("SELECT ca.*, r.RealName AS patientName " +
            "FROM checkapply ca " +
            "JOIN register r ON ca.RegistID = r.ID " +
            "ORDER BY ca.IsUrgent DESC, ca.CreationTime ASC")
    List<Checkapply> selectAllChecksWithPatientName();

    @Select("SELECT ca.*, r.RealName AS patientName " +
            "FROM checkapply ca " +
            "JOIN register r ON ca.RegistID = r.ID " +
            "JOIN fmeditem f ON ca.ItemID = f.ID " +
            "WHERE f.deptid = #{deptId} " +
            "ORDER BY ca.IsUrgent DESC, ca.CreationTime ASC")
    List<Checkapply> selectChecksByDeptWithPatientName(@Param("deptId") Integer deptId);


}